package data.dblp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FormatToTung {

	static final String pathFolder = "/home/quyet/experiments/partition/dblp/1600000";
	static HashMap<Integer, HashSet<String>> listAllOut = new HashMap<Integer, HashSet<String>>();
	static HashMap<Integer, HashSet<String>> listAllIn = new HashMap<Integer, HashSet<String>>();

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		RunFormat();

		try {
			FileWriter fwPart = new FileWriter(pathFolder + "/crosslinks");
			BufferedWriter bwPart = new BufferedWriter(fwPart);

			for (Integer key : listAllOut.keySet()) {
				HashSet<String> listOut = listAllOut.get(key);

				for (String s : listOut) {
					for (Integer keyOut : listAllIn.keySet()) {
						if (!keyOut.equals(key)) {
							HashSet<String> listIn = listAllIn.get(keyOut);
							if (listIn.contains(s)) {
								bwPart.write(s + "_ " + s + " " + key + " "
										+ keyOut + "\n");
							}
						}
					}
				}
			}
			bwPart.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// RunAllWork("/home/quyet/multicores");
		// for (HashSet<String> s : list.values()) {
		// for (String r : s)
		// System.out.println(r);
		// }
		// System.out.println("Finish");
	}

	static HashMap<String, HashSet<String>> list = new HashMap<String, HashSet<String>>();

	public static void MyWork(String pathFile) {
		HashSet<String> listOut = new HashSet<String>();
		try {
			FileReader f = new FileReader(pathFile);
			BufferedReader bNode = new BufferedReader(f);
			String strLine = null;
			while ((strLine = bNode.readLine()) != null) {
				listOut.add(strLine);
			}
			list.put(pathFile, listOut);
			bNode.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void RunAllWork(String pathFolder) {
		int nrOfProcessors = Runtime.getRuntime().availableProcessors();

		System.out.println("Available Processors: " + nrOfProcessors);

		ExecutorService eservice = Executors.newFixedThreadPool(nrOfProcessors);

		for (int i = 1; i <= 4; i++) {
			final String file = pathFolder + "/" + i;
			eservice.submit(new Runnable() {
				@Override
				public void run() {
					MyWork(file);
				}
			});
		}

		eservice.shutdown();
		while (!eservice.isTerminated()) {
		}

	}

	public static void RunFormat() {

		try {

			int nrOfProcessors = Runtime.getRuntime().availableProcessors();

			System.out.println("Available Processors: " + nrOfProcessors);

			ExecutorService eservice = Executors
					.newFixedThreadPool(nrOfProcessors);

			for (int i = 1; i <= 32; i++) {
				final int key = i;
				eservice.submit(new Runnable() {
					@Override
					public void run() {
						Format(key);
					}
				});
				System.out.println("Running at file number: " + key);
			}

			eservice.shutdown();
			while (!eservice.isTerminated()) {
			}
			System.out.println("Finish");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void Format(int key) {
		try {
			String file = "";
			if ((key - 1) < 10)
				file = "0000" + (key - 1);
			else
				file = "000" + (key - 1);

			FileReader f = new FileReader(pathFolder + "/output/part-m-" + file);
			BufferedReader bNode = new BufferedReader(f);
			String strLine = null;

			HashSet<String> listIn = new HashSet<String>();
			HashSet<String> listOut = new HashSet<String>();
			HashSet<String> listAllNode = new HashSet<String>();
			StringBuilder listData = new StringBuilder();

			while ((strLine = bNode.readLine()) != null) {
				if (strLine.length() > 1) {
					String[] row = strLine.split("\t");
					String[] node = row[0].split(":");

					if (node[1].equals("1")) {
						listIn.add(node[0]);
					} else if (node[1].equals("2")) {
						listOut.add(node[0]);
					}
					listAllNode.add(strLine);

				}
			}

			bNode.close();

			listAllOut.put(key, listOut);
			listAllIn.put(key, listIn);

			for (String line : listAllNode) {
				String[] cells = line.split("\t");
				String[] nodes = cells[0].split(":");

				String tmp = cells[0] + ";";
				if (listOut.contains(nodes[0]))
					tmp = nodes[0] + "_:0;";
				else {
					if (!listIn.contains(nodes[0]))
						tmp = nodes[0] + ":2;";

					if (cells.length > 1) {
						for (int i = 1; i < 5; i++) {
							if(cells[i].contains(";"))
								cells[i] = cells[i].replace(";", "#");
							tmp += cells[i] + " \t";
						}
						tmp += cells[5]; // remove tab at last

						for (int i = 6; i < cells.length; i++) {
							if (listIn.contains(cells[i]))
								tmp += ";" + cells[i] + ":1";
							else if (listOut.contains(cells[i]))
								tmp += ";" + cells[i] + "_:0";
							else
								tmp += ";" + cells[i] + ":2";
						}
					}
				}
				listData.append(tmp + "\n");
			}

			FileWriter fwPart = new FileWriter(pathFolder + "/output2/" + key);
			BufferedWriter bwPart = new BufferedWriter(fwPart);
			bwPart.write(listData.toString());
			bwPart.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
